;(function(){

    class Goods{
        constructor(){
            this.bigBox = document.querySelector(".recommend_goods");
        }
        getData(){
            var that = this;
            var p1 = pAjax({
                url:"http://localhost/XMYP/src/php/goods.php",
                type:"post",
            });
            p1.then(function(res){
                that.data = JSON.parse(res);
                that.addElement();
            })
        }
        addElement(){
            var str = '';
            for(var i = 0 ; i < this.data.length ; i++){
                var disappearColor = "block";  //判断是否有颜色可选
                var disappearSpc = "inline-block";     //判读是否是特价
                var disappearGive = "inline-block";     //判读是否有赠送
                var disappearSale = "inline-block";     //判读是否是预售
                var src = '';
                if(!this.data[i].special){
                    disappearSpc = "none";
                }
                if(!this.data[i].give){
                    disappearGive = "none";
                }
                if(!this.data[i].advancesale){
                    disappearSale = "none";
                }else{
                    src = `src ='${this.data[i].advancesale}';`
                }
                if(!this.data[i].color){
                    disappearColor = "none";
                }
                // 判断是否起
                if(!this.data[i].moneyend){
                    this.data[i].moneyend='';
                }

                str += `<article class="recommend_goods_box" goodsid=${this.data[i].goodsid}>
                <div class="recommend_gbox_t">
                    <div>
                        <img abc="${this.data[i].address}" class="lazy" alt="">
                        <p class="recommend_gbox_tcolor" style="display:${disappearColor}">${this.data[i].color}</p>
                    </div>
                    <p>${this.data[i].introduce}</p>
                </div>
                <div class="recommend_gbox_b">
                    <div id="discount"><span class = "discount1" style="display:${disappearSpc};">${this.data[i].special}</span><span class = "discount2" style="display:${disappearGive};">${this.data[i].give}</span><span class = "discount3" style="display:${disappearSale}"><img ${src}}"></span></div>
                    <p>${this.data[i].title}</p>
                    <p>
                        <span>￥</span>
                        <span>${this.data[i].money}</span>
                        <span>${this.data[i].moneyend}</span>
                    </p>
                </div>
            </article>`
            }
            this.bigBox.innerHTML = str;
            goods.addEvent();
        }
        addEvent(){
            var listGoodsBox = $(".recommend_goods_box");
            for(let i = 0 ; i < listGoodsBox.length ; i++){
                listGoodsBox.eq(i).on("click",function(){
                    var str = listGoodsBox.eq(i).attr("goodsid");
                    window.location.href=`http://localhost/XMYP/src/pages/details.html?goodsid=${str}`    
                })
            }

             // 懒加载
             var imgs = $(".lazy");
             var clientH = document.documentElement.clientHeight;
             lazyImg(imgs,clientH);
             onscroll = function(){
                 lazyImg(imgs,clientH);
             }
            
            
        }
    }

    var goods = new Goods();
    goods.getData();



}());